package com.smartleanx.module.hrm.controller.app.dingding;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.module.hrm.controller.admin.dingDing.bo.DingAttendanceConfirmationBo;
import com.smartleanx.module.hrm.controller.admin.dingDing.bo.DingAttendanceRecordBo;
import com.smartleanx.module.hrm.controller.admin.dingDing.bo.DingConfirmationBo;
import com.smartleanx.module.hrm.controller.admin.dingDing.vo.SystemFrameVo;
import com.smartleanx.module.hrm.dal.dataobject.dingDing.DingAttendanceConfirmation;
import com.smartleanx.module.hrm.service.dingDing.DingUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台/员工pc -钉钉用户")
@RestController
@RequestMapping("/hrm/app/ding_user")
@Validated
public class DingUserAppController {
    @Autowired
    private DingUserService dingUserService;

    @GetMapping("/getBySelect")
    @Operation(summary = "获得当前租户下的钉钉员工")
    public CommonResult<Object> geBySelect() {
        List<SystemFrameVo> systemFrameVos = new ArrayList<>();
        SystemFrameVo dingUserSelect = dingUserService.getFrameByDepartmentId(1, true);
        systemFrameVos.add(dingUserSelect);
        return success(systemFrameVos);
    }

    @GetMapping("/bdDingUser")
    @Operation(summary = "绑定钉钉员工")
    public CommonResult<String> bdDingUser(@RequestParam(value = "employeeId") Integer employeeId, @RequestParam(value = "dingUserId") Integer dingUserId) {
        Integer result = dingUserService.bdDingUser(employeeId, dingUserId);
        return success("绑定成功");
    }

    @GetMapping("/removeBdDingUser")
    @Operation(summary = "绑定钉钉员工")
    public CommonResult<String> removeBdDingUser(@RequestParam(value = "employeeId") Integer employeeId) {
        Integer result = dingUserService.removeBdDingUser(employeeId);
        return success("删除成功");
    }

    @PostMapping("/selectList")
    public CommonResult<Object> selectList(@RequestBody DingAttendanceRecordBo dingAttendanceRecordBo) {
        PageInfo dingAttendanceRecordVos = dingUserService.selectList(dingAttendanceRecordBo);
        return success(dingAttendanceRecordVos);
    }

    @GetMapping("/selectTeamList")
    public CommonResult<Object> selectTeamList(@RequestParam(value = "teamName") String teamName) {
        List<JSONObject> jsonObjectList = dingUserService.selectTeamList(teamName);
        return success(jsonObjectList);
    }

    @PostMapping("/selectColumnList")
    public CommonResult<Object> selectColumnList(@RequestBody DingAttendanceRecordBo dingAttendanceRecordBo) {
        JSONObject jsonObject = dingUserService.selectColumnList(dingAttendanceRecordBo);
        return success(jsonObject);
    }

    @PostMapping("/selectColumnMonthList")
    public CommonResult<Object> selectColumnMonthList(@RequestBody DingAttendanceRecordBo dingAttendanceRecordBo) {
        JSONObject jsonObject = dingUserService.selectColumnMonthList(dingAttendanceRecordBo);
        return success(jsonObject);
    }

    @PostMapping("/generateColumn")
    public CommonResult<Object> generateColumn(@RequestBody DingAttendanceConfirmationBo dingAttendanceConfirmationBo) {
        dingUserService.generateColumn(dingAttendanceConfirmationBo);
        return success("生成成功");
    }

    @PostMapping("/updateGenerateColumn")
    public CommonResult<Object> UpdateGenerateColumn(@RequestBody DingAttendanceConfirmationBo dingAttendanceConfirmationBo) {
        dingUserService.UpdateGenerateColumn(dingAttendanceConfirmationBo);
        return success("修改设置成功");
    }

    @PostMapping("/updateConfirmationInfo")
    public CommonResult<Object> updateConfirmationInfo(@RequestBody JSONObject param) {
        dingUserService.updateConfirmationInfo(param);
        return success("修改个人考勤详情成功");
    }

    @PostMapping("/selectListDingAttendanceConfirmation")
    public CommonResult<Object> selectListDingAttendanceConfirmation(@RequestBody DingConfirmationBo dingConfirmationBo) {
        PageInfo<DingAttendanceConfirmation> dingAttendanceConfirmationPageInfo = dingUserService.selectListDingAttendanceConfirmation(dingConfirmationBo);
        return success(dingAttendanceConfirmationPageInfo);
    }

    @GetMapping("/selectByColumn")
    public CommonResult<Object> selectByColumn() {
        return success(dingUserService.selectByColumn());
    }

    @GetMapping("/selectByConfirmationColumn")
    public CommonResult<Object> selectByConfirmationColumn(@RequestParam(value = "confirmationId") Integer confirmationId) {
        return success(dingUserService.selectByConfirmationColumn(confirmationId));
    }

    @GetMapping("/selectById")
    public CommonResult<Object> selectById(@RequestParam(value = "confirmationId") Integer confirmationId, @RequestParam(value = "employeeId") Integer employeeId) {
        return success(dingUserService.selectById(confirmationId, employeeId));
    }


    @GetMapping("/selectByConfirmation")
    public CommonResult<Object> selectByConfirmation(@RequestParam(value = "workDate") String workDate) {
        return success(dingUserService.selectByConfirmation(workDate));
    }


    @GetMapping("/selectByConfirmationOne")
    public CommonResult<Object> selectByConfirmationOne(@RequestParam(value = "confirmationId") Integer confirmationId) {
        return success(dingUserService.selectByConfirmationOne(confirmationId));
    }


    @GetMapping("/sendAllConfirmation")
    public CommonResult<Object> sendAllConfirmation(Integer id) {
        return success(dingUserService.sendAllConfirmation(id));
    }


    @GetMapping("/sendConfirmation")
    public CommonResult<Object> sendConfirmation(Integer id) {
        return success(dingUserService.sendConfirmation(id));
    }



    @GetMapping("/withdrawConfirmation")
    public CommonResult<Object> withdrawConfirmation(Integer id) {
        return success(dingUserService.withdrawConfirmation(id));
    }



    @GetMapping("/withdrawAllConfirmation")
    public CommonResult<Object> withdrawAllConfirmation(Integer id) {
        return success(dingUserService.withdrawAllConfirmation(id));
    }


    @GetMapping("/checkConfirmation")
    public CommonResult<Object> checkConfirmation(Integer id) {
        return success(dingUserService.checkConfirmation(id));
    }


    @GetMapping("/confirmConfirmation")
    public CommonResult<Object> confirmConfirmation(Integer id,String message,Integer status ) {
        dingUserService.confirmConfirmation(id,message,status);
        return success("操作成功");
    }


    @GetMapping("/selectConfirmationByEmployee")
    public CommonResult<Object> selectConfirmationByEmployee(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
        int i = SecurityFrameworkUtils.getLoginUserId().intValue();

        return success(dingUserService.selectConfirmationByEmployee(i,pageNum,pageSize));
    }


    @GetMapping("/selectUpdateConfirmationById")
    public CommonResult<Object> selectUpdateConfirmationById(Integer id){
        dingUserService.selectUpdateConfirmationById(id);
        return success("操作成功");
    }


    @GetMapping("/removeConfirmation")
    public CommonResult<Object> removeConfirmation(Integer id){
        dingUserService.removeConfirmation(id);
        return success("操作成功");
    }

    @GetMapping("/app/selectById")
    public CommonResult<Object> appSelectById(@RequestParam(value = "confirmationId") Integer confirmationId) {
        int i = SecurityFrameworkUtils.getLoginUserId().intValue();
        return success(dingUserService.selectEmployeeById(confirmationId,i));
    }

    @GetMapping("/selectConfirmExcel")
   public void selectConfirmExcel(HttpServletResponse response, Integer confirmationId){
        Map<String, List<List<String>>> export = dingUserService.selectConfirmExcel(confirmationId);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("数据导出.xlsx", "utf-8"));
            EasyExcel.write(response.getOutputStream()).sheet("考勤详情").head(export.get("header"))
                    .doWrite(export.get("data"));
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}
